//
//  RootViewController.m
//  day10作业
//
//  Created by ....... on 15-6-7.
//  Copyright (c) 2015年 qianfeng01. All rights reserved.
//

#import "RootViewController.h"
#import "ImageViewController.h"

@interface RootViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_tableView;
    NSMutableArray *_dataArray;
    NSArray *_sectionTitles;
}
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _sectionTitles = [[NSArray alloc] initWithObjects:@"圣斗士",@"海贼",@"火影忍者",@"美女", nil];
    [self setTableView];
    [self setData];
}

- (void)setTableView {
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
}

- (void)setData {
    _dataArray = [[NSMutableArray alloc] init];
    NSString *filePath1 = [[NSBundle mainBundle] pathForResource:@"image_圣斗士" ofType:@"plist"];
    NSArray *ary1 = [NSArray arrayWithContentsOfFile:filePath1];
    [_dataArray addObject:ary1];
    
    NSString *filePath2 = [[NSBundle mainBundle] pathForResource:@"image_海贼" ofType:@"plist"];
    NSArray *ary2 = [NSArray arrayWithContentsOfFile:filePath2];
    [_dataArray addObject:ary2];
    
    NSString *filePath3 = [[NSBundle mainBundle] pathForResource:@"image_火影忍者" ofType:@"plist"];
    NSArray *ary3 = [NSArray arrayWithContentsOfFile:filePath3];
    [_dataArray addObject:ary3];
    
    NSString *filePath4 = [[NSBundle mainBundle] pathForResource:@"image_美女" ofType:@"plist"];
    NSArray *ary4 = [NSArray arrayWithContentsOfFile:filePath4];
    [_dataArray addObject:ary4];

    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *ary = _dataArray[section];
    return ary.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    NSArray *array = _dataArray[indexPath.section];
    cell.textLabel.text = [array[indexPath.row] objectForKey:@"imageName"];
    cell.detailTextLabel.text = [array[indexPath.row] objectForKey:@"imageInfo"];
    cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:12.0];
    cell.imageView.image = [UIImage imageNamed:cell.textLabel.text];
    if (indexPath.row%2 == 0) {
        cell.backgroundColor = [UIColor colorWithRed:0.5 green:0 blue:0.5 alpha:0.7];
    } else {
        cell.backgroundColor = [UIColor grayColor];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 30;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [NSString stringWithFormat:@"组头:%@",_sectionTitles[section]];
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    return [NSString stringWithFormat:@"组尾:%@",_sectionTitles[section]];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ImageViewController *controller = [[ImageViewController alloc] init];
    NSArray *array = _dataArray[indexPath.section];
    UIImage *image  = [UIImage imageNamed:[array[indexPath.row] objectForKey:@"imageName"]];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    controller.imageView = imageView;
    [self.navigationController pushViewController:controller animated:YES];
    [controller release];
}

- (void)dealloc
{
    [_sectionTitles release];
    [_tableView release];
    [_dataArray release];
    [super dealloc];
}

@end
